package com.panopset.sf;

public abstract class WindocField {

	protected abstract String getGuiValue();
	protected abstract void setGuiValue(final String value);


    /**
     * Validation of field value.
     * @param valueToCheck Value to check.
     * @return true iff valid.
     */
    protected boolean isValid(final String valueToCheck) {
        return true;
    }

    public void setValue(String value) {
		setGuiValue(value);
	}
    
    public String getValue() {
    	return getGuiValue();
    }
    
	protected void saveValue(final String value) {
		if (panVal == null) {
			return;
		}
		panVal.setValue(value);
	}
	
	public void setToDefaults() {
		if (panVal == null) {
			return;
		}
		setGuiValue(panVal.getDefault());
		panVal.setValue(panVal.getDefault());
	}


	public WindocField(Windoc windoc, String defaultValue, final String key) {
		wd = windoc;
		if (wd != null) {
			panVal = wd.register(this, defaultValue, key);
		}
	}

	protected final Windoc wd;
	private PanVal panVal;
	public void load() {
		setGuiValue(getPanValue());
	}

	public void save() {
		if (panVal == null) {
			return;
		}
		panVal.setValue(getGuiValue());
	}

	public String getPanValue() {
		if (panVal == null) {
			return "";
		}
		return panVal.getValue();
	}
}
